#ifndef _LINUX_CDEV_DEV_H
#define _LINUX_CDEV_DEV_H

#include <linux/types.h>
#include <linux/cdev.h>
#include <linux/device.h>

#define CDEV_DRV_VERSION(a,b,c)             ((a << 24) + (b << 16) + (c))

/* 字符设备 */
struct cdev_dev_t {
    void *parent;                           /* 父设备指针 */
    struct device *dev;                     /* 设备指针 */
    char name[32];                          /* 设备名称，can0 */
    int index;                              /* 索引号 */
    struct cdev cdev;                       /* linux 字符设备 */
    const struct file_operations *fops;     /* 文件操作集指针 */
    dev_t devno;                            /* 完整设备号 */
};

/* 字符设备控制器 */
struct cdev_ctrl_t {
    void *parent;                           /* 父设备指针 */
    char name[32];                          /* 设备类名称 */
    struct class *cclass;                   /* 共享类：统一管理所有设备的 /sys/class 接口 */
    int max_num;                            /* 设备最大数量，max_num≥dev_num，方便多板卡扩展 */
    dev_t major_devno;                      /* 主设备号 */
    struct cdev_dev_t *cdev_dev;            /* 字符设备 */
};

/* 初始化字符设备的类和申请足够的设备号 */
int init_cdev_ctrl(void *parent, struct cdev_ctrl_t *ctrl,
                   const char *name, u8 max_num);
/* 注销字符设备的类和所有申请的设备号*/
void exit_cdev_ctrl(struct cdev_ctrl_t *ctrl);
/* 初始化字符设备 */
int init_cdev_dev(void *parent, struct class *cdev_class, struct cdev_dev_t *cdev_dev, 
                  const struct file_operations *fops, dev_t major_devno,
                  const char *name, u8 index);
/* 注销字符设备 */
void exit_cdev_dev(struct class *cdev_class, struct cdev_dev_t *cdev_dev);

#endif /* _LINUX_CDEV_DEV_H */
